#pragma once

#include "kernel/domain/map.h"
#include "kernel/interfaces/Result.h"
#include "kernel/interfaces/ILogger.h"
#include <QString>
#include <QObject>
#include <memory>

namespace infrastructure {

/**
 * @brief 无状态的地图文件读取服务
 * 只负责将 .smap 文件解析为 kernel::Map 对象
 */
class MapService : public QObject {
    Q_OBJECT

public:
    explicit MapService(kernel::ILogger* logger = nullptr, QObject* parent = nullptr);
    ~MapService() = default;

    // Core map loading functionality - 从文件加载地图并返回 Map 对象
    kernel::Result<std::shared_ptr<kernel::Map>> loadMapFromFile(const QString& filePath);

    // Save map to file
    kernel::Result<void> saveMapToFile(const QString& filePath, const kernel::Map& map) const;

signals:
    void mapLoadFinished();
    void mapLoadFailed(const QString& error);

private:
    kernel::ILogger* m_logger;

    // Private helper methods for parsing
    kernel::Result<std::shared_ptr<kernel::Map>> parseJsonMap(const QJsonObject& jsonObject);

    // JSON parsing helpers
    kernel::StationPoint parseStationPoint(const QJsonObject& pointObj) const;
    kernel::BezierPath parseBezierPath(const QJsonObject& pathObj) const;
    kernel::ArcPath parseArcPath(const QJsonObject& pathObj) const;
    kernel::StraightPath parseStraightPath(const QJsonObject& pathObj) const;
    kernel::Area parseArea(const QJsonObject& areaObj) const;
    kernel::MapProperty parseProperty(const QJsonObject& propObj) const;
    kernel::Position parsePosition(const QJsonObject& posObj) const;
};

} // namespace infrastructure